﻿using System;
using System.Collections.Generic;
using System.Text;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;

namespace Jim.Obsidian.Web.Api.Client
{
    public class ApiResult
    {

        private DateTime serverDateTime = DateTime.MinValue;
        private Dictionary<string, ApiSubResult> dictSubResults = new Dictionary<string, ApiSubResult>();

        private DateTime ServerDateTime
        {
            get { return this.serverDateTime; }
        }


        public ApiResult(JObject jo, DateTime serverDateTime)
        {
            this.serverDateTime = serverDateTime;
            JToken jtData = jo["d"];
            if (jtData != null)
            {
                JArray jarr = (JArray)jtData;
                foreach (JToken jtSub in jarr)
                {
                    JObject o = (JObject)jtSub;
                    ApiSubResult asr = new ApiSubResult(this, o);
                    dictSubResults.Add(asr.Name, asr);
                }
            }
        }

        public ApiSubResult GetSubResult(string name)
        {
            if (dictSubResults.ContainsKey(name))
                return dictSubResults[name];
            else
                return null;
        }

    }
}
